﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace VideoClipNameHelper
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public class ScreenModes
    {
        //====================================================================================================================
        //------------------------------------------------------------------
        public static void DoEvents()
        {
            //http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.pushframe.aspx
            System.Windows.Threading.DispatcherFrame frame = new System.Windows.Threading.DispatcherFrame();
            //System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(ExitFrame), frame);
            System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(((o) => { ((System.Windows.Threading.DispatcherFrame)o).Continue = false; return null; })), frame);
            System.Windows.Threading.Dispatcher.PushFrame(frame);
        }
        /*
        private void doEvents()
        {
            //http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.pushframe.aspx
            System.Windows.Threading.DispatcherFrame frame = new System.Windows.Threading.DispatcherFrame();
            System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(ExitFrame), frame);
            System.Windows.Threading.Dispatcher.PushFrame(frame);

            //Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(delegate { }));
            //Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Threading.ThreadStart(() => System.Threading.Thread.Sleep(1))); 

            //System.Windows.Forms.Application.DoEvents();
        }
        public object ExitFrame(object f)
        {
            ((System.Windows.Threading.DispatcherFrame)f).Continue = false;
            return null;
        }
        */
        //====================================================================================================================
        //====================================================================================================================
        private static bool isInitialized = false;
        private static System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer() { Interval = new System.TimeSpan(0, 0, 0, 0, 100), IsEnabled = false };
        //private static System.Timers.Timer tt = new System.Timers.Timer() { Interval= (new System.TimeSpan(0, 0, 0, 0, 100)).TotalMilliseconds, AutoReset=false, Enabled=false };
        private static Window w;
        private static void dt_Tick(object sender, System.EventArgs e)
        {
            dt.IsEnabled = false;
            //tt.Enabled = false;
            //
            w.ShowInTaskbar = true;
            w.Activate();
            w.WindowState = System.Windows.WindowState.Normal;
            w.WindowStyle = System.Windows.WindowStyle.None;
            w.WindowState = System.Windows.WindowState.Maximized;
            //
        }
        private static void Tt_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            dt_Tick(null, null);
        }
        //------------------------------------------------------------------
        public static void EnterFullScreenMode(Window w1)
        {
            //
            if (!isInitialized)
            {
                isInitialized = true;
                dt.Tick += dt_Tick;
                //tt.Elapsed += Tt_Elapsed;
            }
            //
            w = w1;
            dt.IsEnabled = true;
            //tt.Enabled = true;
        }
        /*
public void EnterFullScreenMode()
{
   this.ShowInTaskbar = true;
   this.Activate();
   this.WindowState = System.Windows.WindowState.Normal;
   this.WindowStyle = System.Windows.WindowStyle.None;
   this.WindowState = System.Windows.WindowState.Maximized;
}
*/
        public static void LeaveFullScreenMode(Window w1)
        {
            //
            if (!isInitialized)
            {
                isInitialized = true;
                dt.Tick += dt_Tick;
                //tt.Elapsed += Tt_Elapsed;
            }
            dt.IsEnabled = false;
            //tt.Enabled = false;
            //
            w = w1;
            w.Topmost = true;
            //
            w.WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
            w.WindowState = System.Windows.WindowState.Normal;
            //
            w.Topmost = false;
        }


        //====================================================================================================================
    }
}
